package cn.edu.sdjzu.xg2103.service;

import cn.edu.sdjzu.xg2103.controller.helper.JdbcHelper;
import cn.edu.sdjzu.xg2103.dao.DepartmentDao;
import cn.edu.sdjzu.xg2103.entity.Department;
import cn.edu.sdjzu.xg2103.exception.BysjException;

import cn.edu.sdjzu.xg2103.entity.Department;

import java.sql.Connection;
import java.sql.SQLException;

public class DepartmentService {
    private static  final DepartmentDao departmentDao =DepartmentDao.getInstance();
    private static DepartmentService departmentService = null;
    //将唯一构造器设置成私有，阻止在本类外生成本类的对象
    public static DepartmentService getInstance(){
        //静态变量SchoolDao为null时，才创建SchoolDao对象
        //第二次或后来调用本方法就不指向null了，就不在创建对象。
        if (departmentService == null){
            departmentService = new DepartmentService();
        }
        return departmentService;
    }
    public Department find(int id) throws SQLException {
        Department department = DepartmentDao.getInstance().find(id);
        return department;
    }
    public boolean update(Department department)throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        boolean updated = departmentDao.update(connection,department);
        JdbcHelper.close(connection);
        return updated;
    }
}
